<template>
  <div class="announcement-container">
    <div class="top-item-group">
      <el-date-picker
        v-model="value1"
        type="daterange"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
      ></el-date-picker>
      <div class="search-item-group">
        <el-input style="width:200px" placeholder="请输入公告标题" v-model="searchKeyword" clearable></el-input>
        <el-button type="primary">搜索</el-button>
      </div>
      <div class="bottom-button-group">
        <el-button @click="addDialogVisible=true" type="primary">添加公告</el-button>
        <el-button @click="handlePartialDelete">批量删除</el-button>
        <el-button>导出当前数据</el-button>
      </div>
    </div>

    <div class="middle-table-gorup">
      <el-table
        ref="multipleTable"
        :header-cell-style="{'background-color':'#e6f1ff','height':'62px'}"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        v-loading="isLoading"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column prop="title" label="公告标题" align="center"></el-table-column>
        <el-table-column prop="content" label="公告内容" align="center"></el-table-column>
        <el-table-column prop="col3" label="发布人" align="center">客户中心</el-table-column>
        <el-table-column prop="createTime" label="发布时间" align="center"></el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button @click="getEditForm(scope.row)" type="text" size="normal">编辑</el-button>
            <el-button @click="getDeleteForm(scope.row)" type="text" size="normal">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="bottom-pagination-group">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="pageSize"
          layout="total, prev, pager, next,sizes, jumper"
          :total="totalData"
        ></el-pagination>
      </div>
    </div>

    <el-dialog title="删除确认" :visible.sync="deleteDialogVisible" top="34vh" width="26%">
      <div class="delete-dialog-style">删除后无法恢复，确定删除此信息吗？</div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="deleteDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleDeleteAnnouncement">确定</el-button>
      </span>
    </el-dialog>

    <el-dialog title="添加公告" top="9vh" :visible.sync="addDialogVisible" width="33%">
      <div class="add-dialog-style">
        <el-form
          ref="addAnnouncementForm"
          :rules="rules"
          :model="addAnnouncementForm"
          label-position="left"
          label-width="120px"
        >
          <el-form-item label="公告标题" prop="title">
            <el-input style="width:100%;" v-model="addAnnouncementForm.title" placeholder="请输入公告标题"></el-input>
          </el-form-item>
          <el-form-item label="公告内容" prop="content">
            <el-input
              type="textarea"
              :rows="10"
              v-model="addAnnouncementForm.content"
              placeholder="请输入公告内容"
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native.prevent="resetForm" type="text" size="normal">清空</el-button>
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleCreateAnnouncement">保存</el-button>
      </div>
    </el-dialog>

    <el-dialog title="编辑公告" top="9vh" :visible.sync="editDialogVisible" width="33%">
      <div class="add-dialog-style">
        <el-form
          ref="editAnnouncementForm"
          :rules="rules"
          :model="editAnnouncementForm"
          label-position="left"
          label-width="120px"
        >
          <el-form-item label="公告标题" prop="title">
            <el-input
              style="width:100%;"
              v-model="editAnnouncementForm.title"
              placeholder="请输入公告标题"
            ></el-input>
          </el-form-item>
          <el-form-item label="公告内容" prop="content">
            <el-input
              type="textarea"
              :rows="10"
              v-model="editAnnouncementForm.content"
              placeholder="请输入公告内容"
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <!-- <el-button @click.native.prevent="resetForm" type="text" size="normal">清空</el-button> -->
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleUpdateAnnouncement">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import api from "@/api/api";
export default {
  name: "Announcement",
  components: {},
  props: {},
  data() {
    return {
      tableData: [],
      value1: "",
      currentPage: 1,
      pageSize: 10,
      totalData: 0,
      isLoading: false,
      searchKeyword: "",
      multipleSelection: [],
      deleteDialogVisible: false,
      deleteId: 0,
      addDialogVisible: false,
      addAnnouncementForm: {},
      editDialogVisible: false,
      editAnnouncementForm: {},
      rules: {
        title: [{ required: true, message: "请输入公告标题", trigger: "blur" }]
      }
    };
  },
  watch: {},
  computed: {},
  methods: {
    handleSizeChange(size) {
      this.pageSize = size;
      this.getPageData();
    },
    handleCurrentChange(page) {
      this.currentPage = page;
      this.getPageData();
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    getPageData() {
      this.isLoading = true;
      api
        .getAllAnnouncementAPI({
          page: this.currentPage,
          limit: this.pageSize
        })
        .then(res => {
          this.isLoading = false;
          let { error, data } = res.data;
          if (error) {
            this.$message.error("请求接口异常");
          } else {
            this.totalData = data.total;
            this.tableData = data.page;
          }
        });
    },
    handleCreateAnnouncement() {
      this.$refs.addAnnouncementForm.validate(valid => {
        if (valid) {
          this.addDialogVisible = false;
          api
            .createAnnouncementAPI({
              title: this.addAnnouncementForm.title,
              content: this.addAnnouncementForm.content,
              creatorId: this.$store.state.profile.customerServiceId
            })
            .then(res => {
              let { error, data } = res.data;
              if (error) {
                this.$message.error(data);
              } else {
                this.getPageData();
                this.$message({
                  message: "公告创建成功",
                  type: "success"
                });
              }
            });
        } else {
          this.$message.error("表单填写有误");
        }
      });
    },
    resetForm() {
      this.$refs.addAnnouncementForm.resetFields();
    },
    getEditForm(data) {
      this.editDialogVisible = true;
      this.editAnnouncementForm = data;
    },
    handleUpdateAnnouncement() {
      this.$refs.editAnnouncementForm.validate(valid => {
        if (valid) {
          this.editDialogVisible = false;
          api.updateAnnouncementAPI(this.editAnnouncementForm).then(res => {
            let { error, data } = res.data;
            if (error) {
              this.$message.error(data);
            } else {
              this.getPageData();
              this.$message({
                message: "更新公告成功",
                type: "success"
              });
            }
          });
        } else {
          this.$message.error("表单填写有误");
        }
      });
    },
    getDeleteForm(data) {
      this.deleteDialogVisible = true;
      this.deleteId = data.id;
    },
    handleDeleteAnnouncement() {
      this.deleteDialogVisible = false;
      api.deleteAnnouncementAPI(this.deleteId).then(res => {
        let { error, data } = res.data;
        if (error) {
          this.$message.error(data);
        } else {
          this.getPageData();
          this.$message({
            message: "删除公告成功",
            type: "success"
          });
        }
      });
    },
    handlePartialDelete() {
      let params = this.multipleSelection.map(item => {
        return item.id;
      });
      params = params.join(",");
      api.partialDeleteAnnouncementAPI(params).then(res => {
        let { error, data } = res.data;
        if (error) {
          this.$message.error(data);
        } else {
          this.multipleSelection = [];
          this.getPageData();
          this.$message({
            message: "公告删除成功",
            type: "success"
          });
        }
      });
    },
  },
  created() {
    this.getPageData();
  },
  mounted() {}
};
</script>

<style scoped>
.announcement-container {
  background-color: #fff;
  border-radius: 5px;
  padding: 20px;
}
.top-item-group {
  display: flex;
  justify-content: space-around;
  align-items: center;
}
.middle-table-gorup {
  margin-top: 20px;
  width: 100%;
  display: inline-block;
}
.bottom-pagination-group {
  padding: 20px 20px 0;
  float: right;
}
.delete-dialog-style {
  border-top: 1px solid #f2f2f2;
  border-bottom: 1px solid #f2f2f2;
  padding: 20px;
  padding-bottom: 70px;
}
.add-dialog-style {
  height: 350px;
  overflow: auto;
  padding: 20px 10px;
  margin-top: 10px;
  border-top: 1px solid #f2f2f2;
  border-bottom: 1px solid #f2f2f2;
}
</style>
